import { createRouter, RouteRecordRaw, createWebHistory } from 'vue-router'
import Storage from '../common/constants/Storage'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import NotFound from '@//views/NotFound.vue'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: Home.name,
    component: Home
  },
  {
    path: '/login',
    name: Login.name,
    component: Login
  },
  {
    path: '/:catchAll(.*)*',
    name: NotFound.name,
    component: NotFound
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 不需要鉴权页面
const notAuthPages = [Login.name, NotFound.name];

router.beforeEach((to, form, next) => {
  if (notAuthPages.indexOf(to.name as string) === -1 && !Storage.getStorage(Storage.USER)) {
    next({path: '/login'});
  } else {
    next();
  }
});

export default router
